package com.clp.protocol.iec104.apdu;

import com.clp.protocol.core.common.frame.InitializableFrame;
import com.clp.protocol.iec104.definition.ApduType;

public interface Apdu extends InitializableFrame<Apdu> {
    enum PrintOption {
        DESCRIPTION,
        HEX_STRING
    }

    PrintOption printOption = PrintOption.DESCRIPTION;

    /**
     * 该Apdu的类型
     * @return
     */
    ApduType apduType();

    /**
     * 是否为U帧
     * @return
     */
    default boolean isUType() {
        return apduType() == ApduType.UType;
    }

    /**
     * 是否为S帧
     * @return
     */
    default boolean isSType() {
        return apduType() == ApduType.SType;
    }

    /**
     * 是否为I帧
     * @return
     */
    default boolean isIType() {
        return apduType() == ApduType.IType;
    }

    default UApdu castToUType() {
        if (!this.isUType()) throw new RuntimeException("类型转换失败！");
        return (UApdu) this;
    }

    default SApdu castToSType() {
        if (!this.isSType()) throw new RuntimeException("类型转换失败！");
        return (SApdu) this;
    }

    default IApdu castToIType() {
        if (!this.isIType()) throw new RuntimeException("类型转换失败！");
        return (IApdu) this;
    }
}
